// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB

#include <linux/completion.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/mutex.h>
#include <linux/random.h>
#include <linux/igmp.h>
#include <linux/xarray.h>
#include <linux/inetdevice.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <net/route.h>

#include <net/net_namespace.h>
#include <net/netns/generic.h>
#include <net/tcp.h>
#include <net/ipv6.h>
#include <net/ip_fib.h>
#include <net/ip6_route.h>

#include <rdma/rdma_cm.h>
#include <rdma/rdma_cm_ib.h>
#include <rdma/rdma_netlink.h>
#include <rdma/ib.h>
#include <rdma/ib_cache.h>
#include <rdma/ib_cm.h>
#include <rdma/ib_sa.h>
#include <rdma/iw_cm.h>
#include <trace/hooks/rdma_pangea.h>

#include "core_priv.h"
#include "cma_priv.h"
#include "cma_trace.h"

int rdma_query_gid_index(struct rdma_cm_id *id)
{
	if (id->route.addr.dev_addr.sgid_attr == NULL) {
		ofed_log_print(PANGEA_ERR, "The id(%px) is not bind addr.", id);
		return -EINVAL;
	}

	ofed_log_print(PANGEA_DEBUG, "The id(%px) binding addr is %pI6, index:%d, dev:%s.",
		id, id->route.addr.dev_addr.sgid_attr->gid.raw,
		id->route.addr.dev_addr.sgid_attr->index,
		id->route.addr.dev_addr.sgid_attr->device->name);

	return id->route.addr.dev_addr.sgid_attr->index;
}
EXPORT_SYMBOL(rdma_query_gid_index);

int rdma_query_comm_attr(struct rdma_cm_id *id, struct rdma_comm_attr *attr)
{
	struct rdma_id_private *id_priv;

	id_priv = container_of(id, struct rdma_id_private, id);
	if (!rdma_cap_ib_cm(id->device, id->port_num))
		return -EPROTONOSUPPORT;

	attr->local_id = id_priv->cm_id.ib->local_id;
	attr->remote_id = id_priv->cm_id.ib->remote_id;
	attr->tid = ib_cm_get_tid(id_priv->cm_id.ib);
	return 0;
}
EXPORT_SYMBOL(rdma_query_comm_attr);
